package com.yu.jay.controller;

import com.yu.jay.pojo.User;
import com.yu.jay.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller 控制层需要的注解
//@RestController 使用这个也是可以的，但是使用后他里面所有请求返回的都是字符串！
//一般只需要作为接口放回JSON格式数据的话推荐使用@RestController
//@Controller这个是可以与Thymeleaf模板引擎使用时可以返回一个页面的
@RestController
//@RequestMapping指定路径名
//@RequestMapping("/user")用这个来指定路径也是可以的
@RequestMapping("/user")
public class UserController {
    //获取到UserService
    @Autowired
    private UserService userService;

    /**
     * 增加用户
     */
    @PostMapping
    public String addUser(@RequestBody User user) {
        boolean result = userService.addUser(user);
        return result ? "User added successfully" : "Failed to add user";
    }

     /**
     * 删除用户
     */
     @DeleteMapping
     public String delUser(@RequestBody User user) {
         boolean result = userService.delUser(user);
         return result ? "User deleted successfully" : "Failed to delete user";
     }

     /**
     * 修改用户
     */
     @PutMapping
     public String updateUser(@RequestBody User user) {
         boolean result = userService.updateUser(user);
         return result ? "User updated successfully" : "Failed to update user";
     }

     /**
     * 查看用户
     */
     @GetMapping
     public List<User> getUsers(){
         List<User> users = userService.getUsers();
         return users;
     }

}
